// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements.  See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership.  The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License.  You may obtain a copy of the License at
//
//   http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied.  See the License for the
// specific language governing permissions and limitations
// under the License.

// - Access pb services via HTTP
//   ./http_client http://www.foo.com:8765/EchoService/Echo -d '{"message":"hello"}'
// - Access builtin services
//   ./http_client http://www.foo.com:8765/vars/rpc_server*
// - Access www.foo.com
//   ./http_client www.foo.com

#include <turbo/flags/flag.h>
#include <krpc/utility/logging.h>
#include <krpc/rpc/channel.h>
#include <krpc/rpc/restful_client.h>

TURBO_FLAG(std::string, url, "http://localhost:8010/builtin/version", "The algorithm for load balancing");

int main(int argc, char* argv[]) {
    auto *run_app = turbo::Servlet::instance().run_app();
    run_app->add_option("--url", FLAGS_url, FLAGS_url.help());
    auto [ext, ret] = turbo::Servlet::instance().run(argc, argv);
    if (ext) {
        return ret;
    }
    krpc::RestfulClient client;
    //auto task = std::make_shared<krpc::RestfulTask>();
    // task->set_url(url);
    auto task = client.async_get(turbo::get_flag(FLAGS_url));
    auto rs = task->wait();
    if(!rs.ok()) {
        LOG(INFO)<<rs;
        exit(-1);
    }

    std::cout<<task->response().status_code()<<std::endl;
    std::cout<<task->response().body().size()<<std::endl;
    std::cout<<task->response().body().to_string()<<std::endl;
    return 0;
}
